package demo13;

import java.io.*;
import java.net.*;
import java.util.*;

public class TcpEchoClient {
    Socket socket=null;

    TcpEchoClient(String ip,int port) throws IOException {
        socket=new Socket(ip,port);
    }

    public void start(){
        System.out.println("客户端启动");
        Scanner scanner=new Scanner(System.in);
        try(InputStream inputStream= socket.getInputStream();
            OutputStream outputStream=socket.getOutputStream()){
        while(true){
            System.out.print("->");

                Scanner in=new Scanner(inputStream);
                String request=scanner.next();
                PrintWriter writer=new PrintWriter(outputStream);
                writer.println(request);
                writer.flush();


                String respond=in.next();


                System.out.println(respond);

            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) throws IOException {
        TcpEchoClient client=new TcpEchoClient("127.0.0.1",9091);
        client.start();
    }
}
